package com.student.dms;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.TextUtils;
import android.widget.EditText;

import androidx.annotation.Nullable;

import com.hjq.bar.OnTitleBarListener;
import com.hjq.bar.TitleBar;
import com.luck.picture.lib.utils.ToastUtils;
import com.student.db.AccountDao;
import com.student.db.AccountEntity;
import com.student.db.AppDatabase;
import com.student.db.FixReportDao;
import com.student.db.FixReportEntity;
import com.student.dms.databinding.ActivityFixReportBinding;
import com.student.dms.databinding.ActivityLoginBinding;

import java.text.SimpleDateFormat;
import java.util.Date;

import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers;
import io.reactivex.rxjava3.core.Single;
import io.reactivex.rxjava3.schedulers.Schedulers;

public class FixReportActivity extends BaseActivity {

    private ActivityFixReportBinding activityFixReportBinding;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        activityFixReportBinding = ActivityFixReportBinding.inflate(getLayoutInflater());
        setContentView(activityFixReportBinding.getRoot());
        SharedPreferences sharedPreferences = getSharedPreferences("user", Context.MODE_PRIVATE);
        SharedPreferences.Editor edit = sharedPreferences.edit();
        String username = sharedPreferences.getString("username", "");

        activityFixReportBinding.titleBar.setOnTitleBarListener(new OnTitleBarListener() {
            @Override
            public void onLeftClick(TitleBar titleBar) {
                finish();
            }
        });
        activityFixReportBinding.submit.setOnClickListener(v -> {
            String editFixName = activityFixReportBinding.editFixName.getText().toString().trim();
            String editFixReason = activityFixReportBinding.editFixReason.getText().toString().trim();
            if (checkInputIsNull(editFixName) || checkInputIsNull(editFixReason)) {
                ToastUtils.showToast(FixReportActivity.this, "报修物品或报修原因不能为空！");
                return;
            }
            AppDatabase roomDatabase = DMSApplication.getApplication().getRoomDatabase();
            AccountDao accountDao = roomDatabase.provideAccountDao();
            Single<AccountEntity> accountFlow = accountDao.searchAccountByName(username);
            disposable.add(accountFlow.subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(accountEntity -> {
                FixReportEntity fixReportEntity = new FixReportEntity();
                fixReportEntity.setUsername(accountEntity.getUserName());
                fixReportEntity.setFixName(editFixName);
                fixReportEntity.setFixReason(editFixReason);
                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
                String time = simpleDateFormat.format(new Date());
                fixReportEntity.setSubmitTime(time);
                fixReportEntity.setRoomNo(accountEntity.getRoomNo());
                FixReportDao fixReportDao = roomDatabase.provideFixReportDao();
                disposable.add(fixReportDao.insert(fixReportEntity).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(() -> {
                    ToastUtils.showToast(FixReportActivity.this, "报修成功！");
                    FixReportActivity.this.finish();
                }, insertstudentDaothrowable -> ToastUtils.showToast(FixReportActivity.this, "密码更新失败！  message: " + insertstudentDaothrowable.getMessage())));

                ToastUtils.showToast(FixReportActivity.this, "提交成功！");
            }));
        });
    }

    private boolean checkInputIsNull(String text) {
        return TextUtils.isEmpty(text);
    }
}
